<?php
	
	if(ownHooker() == 1)
	{
		$cid = $_SESSION['cid'];
		$q = mysql_query("SELECT * FROM hoe, hoe_own WHERE hoe.id = hoe_own.hid AND hoe_own.cid = '$cid'");
		if(mysql_num_rows($q) == 1)
		{
			$f = mysql_fetch_array($q);
			$hid = $f['hid'];
			$time = time();

			$minpayout = $f['minpollar'];
			$maxpayout = $f['maxpollar'];
			$payout = 0;
			$minutes = $f['payout'];
			$seconds = $minutes*60;

			$q = mysql_query("SELECT * FROM hoe_log WHERE hid='$hid' AND cid='$cid' ORDER BY lasttime DESC");
			$f = mysql_fetch_array($q);

			$difference = $time-$f['lasttime'];
			$times = floor($difference/$seconds);

			if($difference >= $seconds)
			{
				for($i = 0; $i < $times; $i++)
				{
					$randPayout = rand($minpayout, $maxpayout);
					$payout = $payout+$randPayout;
				}

				mysql_query("INSERT INTO hoe_log (hid, cid, lasttime, pollar) VALUES ('$hid', '$cid', '$time', '$payout')");
				logTransaction(3, $payout, TransactionType(3));
				addPollar($payout, getAidFromCid($cid));
			}
		}
	}

	if(isset($_GET['sell']))
	{
		sellHooker($_GET['sell']);
	}

	if(isset($_GET['buy']))
	{
		buyHooker($_GET['buy']);
	}

	function sellHooker($id)
	{
		$q = mysql_query("SELECT * FROM hoe WHERE id='$id'");
		$f = mysql_fetch_array($q);
		$sellprice = round($f['buyprice']/2);
		$aid = $_SESSION['uid'];
		$cid = $_SESSION['cid'];

		addPollar($sellprice, $aid);
		$q = mysql_query("SELECT name FROM hoe WHERE id='$id'");
		$f = mysql_fetch_array($q);
		mysql_query("DELETE FROM hoe_own WHERE hid='$id' AND cid='$cid'");
		alert("Hooker has been sold for $sellprice pollars!");
		redirect("?page=hookers");

		logTransaction(4, $sellprice, TransactionType(4, $f['name']));
	}

	function buyHooker($id)
	{
		$q = mysql_query("SELECT * FROM hoe WHERE id='$id'");
		$f = mysql_fetch_array($q);
		$name = $f['name'];
		
		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		if(removePollar($f['buyprice'], $aid) == 1)
		{
			$q = mysql_query("SELECT * FROM hoe_own WHERE cid='$cid'");
			$time = time();
			if(mysql_num_rows($q) == 1)
			{
				mysql_query("UPDATE hoe_own SET hid='$id' WHERE cid='$cid'");
			}
			else
			{
				mysql_query("INSERT INTO hoe_own (hid, cid) VALUES ('$id', '$cid')");
			}

			mysql_query("INSERT INTO hoe_log (hid, cid, lasttime, pollar) VALUES ('$id', '$cid', '$time', '0')");

			logTransaction(5, $f['buyprice'], TransactionType(5, $name));

			alert("Successfully bought $name");
			redirect("?page=hookers");

		}
		else
		{
			alert("You don't have enough Pollars on your account.");
			redirect("?page=hookers");		
		}
	}

	function getBuyAbleHookers()
	{
		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		$level = getCharLevel($aid);

		$q = mysql_query("SELECT * FROM hoe WHERE minlevel <= '$level' ORDER BY buyprice ASC");
		
		echo '<h3>Available Hooker(s):</h3>';
		while($f = mysql_fetch_array($q))
		{

			$hid = $f['id'];
			$q2 = mysql_query("SELECT * FROM hoe_own WHERE hid='$hid' AND cid='$cid'");

			if(mysql_num_rows($q2) == 0)
			{
				echo '<table style="float:left; margin-right:10px;">';
				echo '<tr>
						<td rowspan="6"><img src="img/hookers/'.$f['picture'].'" width="120px"></td>
						<td width="110" colspan="2"><b>'.$f['name'].'</b></td>
					</tr>
					<tr>
						<td>Earnings:</td><td>&#x2766;'.$f['minpollar'].' - &#x2766;'.$f['maxpollar'].'</td>
					</tr>
					<tr>
						<td>Payout time:</td><td>'.$f['payout'].' Minutes</td>
					</tr>
					<tr>
						<td>Price:</td><td>&#x2766;'.$f['buyprice'].'</td>
					</tr>
					<tr>
						<td>Min-Level:</td><td>'.$f['minlevel'].'</td>
					</tr>
					<tr>
						<td colspan="2"><a href="?page=hookers&buy='.$f['id'].'"><button class="bsexy">Buy '.$f['name'].'</button></a></td>
					</tr>';
				echo '</table>';
			}
		}
		
	}

	function getCurrentHooker()
	{
		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		echo '<h3>Your Hooker:</h3>';

		$q = mysql_query("SELECT * FROM hoe, hoe_own WHERE hoe.id = hoe_own.hid AND hoe_own.cid='$cid'");
		$f = mysql_fetch_array($q);

		$hid = $f['hid'];
		$getInfo = mysql_query("SELECT * FROM hoe_log WHERE cid='$cid' AND hid='$hid'");

		$totalearning = 0;
		$totalpayouts = 0;
		$lastpayout = 0;

		if(mysql_num_rows($getInfo) >= 1)
		{
			$lastpayout = mysql_fetch_array(mysql_query("SELECT lasttime FROM hoe_log WHERE hid='$hid' AND cid='$cid' ORDER BY lasttime DESC LIMIT 1"));
			$lastpayout = floor((time()-$lastpayout['lasttime'])/60);

			while($log = mysql_fetch_array($getInfo))
			{
				$totalearning = $totalearning+$log['pollar'];
				$totalpayouts++;
			}

			$totalpayouts = $totalpayouts-1;
		}
		

		echo '<table>';
				echo '<tr>
						<td rowspan="6"><img src="img/hookers/'.$f['picture'].'" width="150px" height="150px"></td>
						<td width="130"><b>'.$f['name'].'</b></td>
					</tr>
					<tr>
						<td>Earnings:</td><td>&#x2766;'.$f['minpollar'].' - &#x2766;'.$f['maxpollar'].'</td>
					</tr>
					<tr>
						<td>Total Earning:</td><td>&#x2766;'.$totalearning.'</td>
					</tr>
					<tr>
						<td>Payout Time:</td><td>'.$f['payout'].' Minutes</td>
					</tr>
					<tr>
						<td>Total Payouts:</td><td>'.$totalpayouts.' time(s)</td>
					</tr>
					<tr>
						<td>Last Payout:</td><td>'.$lastpayout.' minutes ago</td>
					</tr>
					<tr>
						<td></td><td></td><td><a href="?page=hookers&sell='.$f['hid'].'"><button class="bsexy">Sell for &#x2766;'.round($f['buyprice']/2).'</button></a></td>
					</tr>';
		echo '</table>';
	}

    function getHookers()
	{
		$cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];

		$level = getCharLevel($aid);
		$q = mysql_query("SELECT * FROM hoe_own WHERE cid='$cid'");
		
		if(mysql_num_rows($q) == 1)
		{
			getCurrentHooker();
		}
		else
		{
			echo 'You don\'t own a hooker yet. Be a real pimp and buy one now.<br><br>';
		}

		getBuyAbleHookers();
		
		
	}

	function ownHooker()
	{
		$cid = $_SESSION['cid'];
		$q = mysql_query("SELECT * FROM hoe_own WHERE cid='$cid' ");
		if(mysql_num_rows($q) == 1)
		{
			return 1;
		}
	}




?>